package com.controller;

import com.dto.Result;
import com.entity.BusinessProjectRevenue;
import com.service.BusinessProjectRevenueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/business/revenue")
public class BusinessProjectRevenueController {

    @Autowired
    private BusinessProjectRevenueService businessProjectRevenueService;

    @PostMapping
    public BusinessProjectRevenue createBusinessRevenue(@RequestBody BusinessProjectRevenue businessProjectRevenue) {
        return businessProjectRevenueService.save(businessProjectRevenue);
    }

    @DeleteMapping("/{id}")
    public void deleteBusinessRevenue(@PathVariable Integer id) {
        businessProjectRevenueService.deleteById(id);
    }

    @PutMapping("/{id}")
    public BusinessProjectRevenue updateBusinessRevenue(@PathVariable Integer id, @RequestBody BusinessProjectRevenue businessProjectRevenue) {
        businessProjectRevenue.setId(id);
        return businessProjectRevenueService.update(businessProjectRevenue);
    }

    @GetMapping("/{id}")
    public BusinessProjectRevenue getBusinessRevenueById(@PathVariable Integer id) {

        return businessProjectRevenueService.findById(id);
    }

    @GetMapping
    public List<BusinessProjectRevenue> getAllBusinessRevenues() {
        return businessProjectRevenueService.findAll();
    }

    @GetMapping("/page")
    public Page<BusinessProjectRevenue> getBusinessRevenuesByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        return businessProjectRevenueService.findAll(pageable);
    }
}